package br.fatec.ads.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.fatec.ads.dao.CandidaturaDAO;
import br.fatec.ads.exception.CandidaturaException;
import br.fatec.ads.exception.DataBaseException;
import br.fatec.ads.model.Candidatura;

public class CandidaturaAlterar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CandidaturaAlterar() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			
			Candidatura candidatura = new Candidatura();
			CandidaturaDAO candidaturaDAO = new CandidaturaDAO();
		
			candidatura = candidaturaDAO.getCandidatura(Integer.parseInt(request.getParameter("idCandidaturaAlterar")));
			
			 session.setAttribute("candidatura", candidatura);
			 
			 request.getRequestDispatcher("cadcandidatura_a.jsp").forward(request, response);
		} catch (CandidaturaException e) {
			e.printStackTrace();
		} catch (DataBaseException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			
			Candidatura candidatura = new Candidatura();
			candidatura.setIdCandidatura(Integer.parseInt(request.getParameter("idCandidaturaAlterar")));
			candidatura.setIndAprovado(request.getParameter("indAprovado") == null ? "N" : "S");
			candidatura.setUsuario(session.getAttribute("usuarioDesc").toString());
			
			CandidaturaDAO candidaturaDAO = new CandidaturaDAO();
		
			candidaturaDAO.alterarCandidatura(candidatura);
			
	        request.getRequestDispatcher("CandidaturaPesquisar?acao=SUCESSO_ALTERAR").forward(request, response);
		} catch (CandidaturaException e) {
			request.getRequestDispatcher("CandidaturaPesquisar?acao=ERRO_ALTERAR").forward(request, response);
			e.printStackTrace();
		} catch (DataBaseException e) {
			request.getRequestDispatcher("CandidaturaPesquisar?acao=ERRO_ALTERAR").forward(request, response);
			e.printStackTrace();
		}
	}
}